/*
公共服务类，无状态，提供涉及图形、拓扑处理的公共函数
*/
import LXCOM from '../common/common.js'
import {KeyGenerator}  from '../common/keygenerator.js'

import utilfuncs from '../common/tools/utilfuncs.js';
var funcs = utilfuncs.UtilFuncs;

import globalConst from '../common/globalConst.js';
var global = globalConst.GlobalConst;

var CommonSvc = {}

CommonSvc.calGraphsZone = function (tGraphlst) {
    var rect = new LXCOM.LGXRect(-1, -1, -1, -1);
    let count = tGraphlst.length;
    if (count > 0) {
        var pCntGraph = tGraphlst[0];
        rect = pCntGraph.getZoneInWolrd();
        for (let i = 1; i < count; i++) {
            pCntGraph = tGraphlst[i];
            var tmpRect = pCntGraph.getZoneInWolrd();
            rect.left = Math.min(tmpRect.left, rect.left);
            rect.left = Math.min(tmpRect.right, rect.left);

            rect.right = Math.max(tmpRect.right, rect.right);
            rect.right = Math.max(tmpRect.left, rect.right);

            rect.top = Math.max(tmpRect.top, rect.top);
            rect.top = Math.max(tmpRect.bottom, rect.top);

            rect.bottom = Math.min(tmpRect.bottom, rect.bottom);
            rect.bottom = Math.min(tmpRect.top, rect.bottom);

        }
    }
    return rect;
}


CommonSvc.breakGraph2LineSecs = function (graph, dataManager) {
    let posList = graph.getPos().posList;
    let posCount = posList.length;
    if (posCount < 3) {
        return;
    }
    let info = graph.toInfo();
    let p0 = posList[0];
    for (var i = 1; i < posCount; i++) {
        let p1 = posList[i];
        let pNewLineGraph = this.creatNewLineGraph(p0, p1, info, dataManager);
        pNewLineGraph.setClose(false);
        p0 = p1;
    }
    if (graph.chkClose()) {
        //如果是封闭图形，则要把收尾之间也生成一条新线路
        let p1 = posList[0];
        let pNewLineGraph = this.creatNewLineGraph(p0, p1, info, dataManager);
        pNewLineGraph.setClose(false);
    }
    graph.setUseful(0);
}

CommonSvc.creatNewLineGraph = function (p0, p1, info, dataManager) {
    if (funcs.distancePoint(p0,p1,1) < global.ZERO_4E){
        console.log("zero lenth line ,error******************");
        alert("zero lenth line ,error******************");
        return null;
    }
    let toolKind = global.TwoPtline_noneTopo;
    let pNewLineGraph = dataManager.creatNewGraph4InnerSvc(toolKind);
    if (!pNewLineGraph) {
        return null;
    }
    pNewLineGraph.assignInfo(info);
    pNewLineGraph.m_gid = KeyGenerator.gid();

    let newPosition = pNewLineGraph.getPos();
    newPosition.clear();

    let newPt = new LXCOM.LGXPoint();
    newPt.x = p0.x;
    newPt.y = p0.y;
    newPosition.addPoint(newPt);

    newPt = new LXCOM.LGXPoint();
    newPt.x = p1.x;
    newPt.y = p1.y;
    newPosition.addPoint(newPt);
    pNewLineGraph.calScope(1);
    return pNewLineGraph;
}

CommonSvc.creatNewArcGraph = function (pArcPos, info, dataManager) {
    let toolKind = 0x03200000;
    let pNewArcGraph = dataManager.creatNewGraph4InnerSvc(toolKind);
    if (!pNewArcGraph) {
        return null;
    }
    // pNewArcGraph.assignInfo(info);
    pNewArcGraph.m_gid = KeyGenerator.gid();
    let newPosition = pNewArcGraph.getPos();
    newPosition.clone(pArcPos);
    pNewArcGraph.calControlPoint();
    pNewArcGraph.calScope(1);
    return pNewArcGraph;
}

CommonSvc.copy2CreateNewGraph = function (toolKind,position, info, dataManager) {
    let pNewGraph = dataManager.creatNewGraph4InnerSvc(toolKind);
    if (!pNewGraph) {
        return null;
    }
    pNewGraph.assignInfo(info);
    pNewGraph.m_gid = KeyGenerator.gid();
    let newPosition = pNewGraph.getPos();
    newPosition.clone(position);
    pNewGraph.calScope(1);
    return pNewGraph;
}


CommonSvc.getLineGraphHornVertKind = function (pBusGraph) {
    // CGraphPosBox box;
    // pBusGraph.GetPosCopy(box);
    // MultiPointPos *pMulPos = (MultiPointPos *)box.pos;
    // CustPoint *headpt = pMulPos.getHead();
    // CustPoint *tailpt = pMulPos.getTail();
    // TLineLayoutKind kind = CYGUtilFuncs::checkVertOrHornLnKind(*headpt,*tailpt);
    // return kind;
}

CommonSvc.combineTwoLine = function (connPt, pLineGraph1, pLineGraph2, scaling, mapscale, tToBeDelGraphLst) {
    // CGraphPosBox box1;
    // pLineGraph1.GetPosCopy(box1);
    // MultiPointPos *pLinePos1 = (MultiPointPos *)box1.pos;
    // int poscount1 = pLinePos1.posLst.size();

    // CGraphPosBox box2;
    // pLineGraph2.GetPosCopy(box2);
    // MultiPointPos *pLinePos2 = (MultiPointPos *)box2.pos;
    // int poscount2 = pLinePos2.posLst.size();
    // if (poscount1 < 2 || poscount2 < 2){
    //     return -1;
    // }

    // CustPoint *headpt1 = pLinePos1.posLst.at(0);
    // CustPoint *tailpt1 = pLinePos1.posLst.at(poscount1 - 1);
    // double dis1 = CYGUtilFuncs::DistancePoints(connPt,*headpt1,scaling);
    // double dis2 = CYGUtilFuncs::DistancePoints(connPt,*tailpt1,scaling);

    // CustPoint *headpt2 = pLinePos2.posLst.at(0);
    // CustPoint *tailpt2 = pLinePos2.posLst.at(poscount2 - 1);

    // MultiPointPos tNewLinePos;
    // double dis3 = CYGUtilFuncs::DistancePoints(connPt,*headpt2,scaling);
    // double dis4 = CYGUtilFuncs::DistancePoints(connPt,*tailpt2,scaling);
    // CustPoint *pBeCombineLnAnotherPt = null;
    // if (dis1 < dis2){
    //     //合并headpt1
    //     if (dis3 < dis4){
    //         //合并headpt2
    //         for (int i = poscount1 - 1;i >= 1;i--){
    //             CustPoint *p = pLinePos1.posLst.at(i);
    //             tNewLinePos.posLst.push(new CustPoint(*p));
    //         }

    //         for (int i = 1;i < poscount2;i++){
    //             CustPoint *p = pLinePos2.posLst.at(i);
    //             tNewLinePos.posLst.push(new CustPoint(*p));
    //         }
    //         pBeCombineLnAnotherPt = tailpt2;
    //     }
    //     else{
    //         //合并tailpt2
    //         for (int i = poscount1 - 1;i >= 1;i--){
    //             CustPoint *p = pLinePos1.posLst.at(i);
    //             tNewLinePos.posLst.push(new CustPoint(*p));
    //         }

    //         for (int i = poscount2 - 2;i >= 0;i--){
    //             CustPoint *p = pLinePos2.posLst.at(i);
    //             tNewLinePos.posLst.push(new CustPoint(*p));
    //         }
    //         pBeCombineLnAnotherPt = headpt2;
    //     }
    // }
    // else{
    //     //合并tailpt1
    //     if (dis3 < dis4){
    //         //合并headpt2
    //         for (int i = 0;i <= poscount1 - 2;i++){
    //             CustPoint *p = pLinePos1.posLst.at(i);
    //             tNewLinePos.posLst.push(new CustPoint(*p));
    //         }

    //         for (int i = 1;i < poscount2;i++){
    //             CustPoint *p = pLinePos2.posLst.at(i);
    //             tNewLinePos.posLst.push(new CustPoint(*p));
    //         }
    //         pBeCombineLnAnotherPt = tailpt2;
    //     }
    //     else{
    //         //合并tailpt2
    //         for (int i = 0;i <= poscount1 - 2;i++){
    //             CustPoint *p = pLinePos1.posLst.at(i);
    //             tNewLinePos.posLst.push(new CustPoint(*p));
    //         }

    //         for (int i = poscount2 - 2;i >= 0;i--){
    //             CustPoint *p = pLinePos2.posLst.at(i);
    //             tNewLinePos.posLst.push(new CustPoint(*p));
    //         }
    //         pBeCombineLnAnotherPt = headpt2;
    //     }
    // }

    // ConnectNode *pAnotherNode = null;
    // ConnectLineSegment *pLNRes2 = (ConnectLineSegment*)pLineGraph2.m_pRefTopoResource;
    // int connNd = pLNRes2.getConnNodeCount();
    // if (connNd == 2){
    //     ConnectNode *pNode1 = pLNRes2.getConnNode(0);
    //     CustPoint nodPt1(pNode1.getx(),pNode1.gety());
    //     double dis1 = CYGUtilFuncs::DistancePoints(*pBeCombineLnAnotherPt,nodPt1,scaling);

    //     ConnectNode *pNode2 = pLNRes2.getConnNode(1);
    //     CustPoint nodPt2(pNode2.getx(),pNode2.gety());
    //     double dis2 = CYGUtilFuncs::DistancePoints(*pBeCombineLnAnotherPt,nodPt2,scaling);
    //     pAnotherNode = pNode1;
    //     if (dis1 > dis2 ){
    //         pAnotherNode = pNode2;
    //     }

    //     if (pAnotherNode){
    //         ConnectLineSegment *pLNRes1 = (ConnectLineSegment*)pLineGraph1.m_pRefTopoResource;
    //         pLNRes2.removeNode(pAnotherNode);//断开节点和被合并线路的连接

    //         //断开被合并节点和合并后线路的连接关系
    //         QList<ConnectNode *> tLineConnOldNodeLst = pLNRes1.getConnNodeList();
    //         foreach (ConnectNode *pOldNode, tLineConnOldNodeLst) {
    //             AbsResource *pOldNodeRefRes = pOldNode.getRefRes();
    //             if (pOldNode.getUseful() == 0){
    //                 pLNRes1.removeNode(pOldNode);
    //             }
    //             else if (pOldNodeRefRes){
    //                 CAbsDrawing *pTempGraph = pOldNodeRefRes.getRefGraph();
    //                 if (pTempGraph && tToBeDelGraphLst.indexOf(pTempGraph) != -1){
    //                     pLNRes1.removeNode(pOldNode);
    //                 }
    //             }
    //         }
    //         pAnotherNode.addConnLineSeg(pLNRes1);//把该节点跟合并后线路连接起来
    //     }
    // }

    // //合并坐标到线1
    // pLineGraph1.SetPos(&tNewLinePos);
    // pLineGraph1.GetScope(mapscale);

    // //删除线2
    // pLineGraph2.setUseful(0);
    // pLineGraph2.m_pRefTopoResource.setUseful(0);
    // pLineGraph2.m_pRefTopoResource.setModified(1);
    // pLineGraph2.setModified(1);

    // //断开被合并线路2和关联节点的关系
    // QList<ConnectNode *> ln2ConnNodeLst = pLNRes2.getConnNodeList();
    // pLNRes2.clearNode();
    // foreach (ConnectNode *pNode, ln2ConnNodeLst) {
    //     if (pNode.getConnLineCount() == 0)
    //     {
    //         AbsResource *pNodeRefRes = pNode.getRefRes();
    //         if (pNodeRefRes){
    //             CAbsDrawing *pTempGraph = pNodeRefRes.getRefGraph();
    //             if (pTempGraph){
    //                 if (pTempGraph.m_graphType == CommBusGraph_type){
    //                     PowerBusRes *pBusRes = (PowerBusRes *)pNodeRefRes;
    //                     pBusRes.removeNode(pNode);
    //                     pBusRes.setModified(1);
    //                     pTempGraph.setModified(1);

    //                     pNode.setUseful(0);
    //                     pNode.setNewAdd(0);
    //                     pNode.setModified(1);
    //                 }
    //             }
    //         }
    //         else{
    //             pNode.setUseful(0);
    //             pNode.setNewAdd(0);
    //             pNode.setModified(1);
    //         }
    //     }
    // }
    return 0;
}



CommonSvc.reCorrectConnLine = function (pDevGraph) {
    // if (!pDevGraph.m_pRefTopoResource){
    //     return -1;
    // }
    // ConductingEquipment *pConDuct = (ConductingEquipment*)pDevGraph.m_pRefTopoResource;
    // QList<ConnectNode*> tNodeLst = pConDuct.getConnNodeList();
    // foreach (ConnectNode *pNode, tNodeLst) {
    //     AbsResource *pRefRes = pNode.getRefRes();
    //     if (pRefRes){
    //         if (CYGUtilFuncs::isBusSection(pRefRes.m_nGraphicID)){
    //             reCorrectLineConnBus(pNode,pRefRes,pDevGraph);
    //         }
    //     }
    // }
}

CommonSvc.reCorrectLineConnBus = function (pNode, pBusRefRes, pLineGraph) {
    // CustPoint nodePt = pNode.getPos();
    // CGraphPosBox linebox;
    // pLineGraph.GetPosCopy(linebox);
    // MultiPointPos *pConnLinePos = (MultiPointPos*)linebox.pos;
    // CustPoint *pHeadPt = pConnLinePos.getHead();
    // CustPoint *pTailPt = pConnLinePos.getTail();

    // double dis1 = CYGUtilFuncs::DistancePoints(*pHeadPt,nodePt,1);
    // double dis2 = CYGUtilFuncs::DistancePoints(*pTailPt,nodePt,1);
    // CustPoint *pConn2NodePt = pHeadPt;
    // if (dis2 < dis1){
    //     pConn2NodePt = pTailPt;
    // }

    // CGraphPosBox busbox;
    // pBusRefRes.getRefGraph().GetPosCopy(busbox);
    // MultiPointPos *pBusPos = (MultiPointPos*)busbox.pos;

    // int stIndex = 0;
    // CustPoint interPos = CBaseService::getLineConn2DestLinePos(pBusPos, pConnLinePos, stIndex);
    // pNode.setPosOnly(interPos.x, interPos.y);
    // *pConn2NodePt = interPos;
    // pLineGraph.SetPos(pConnLinePos);
}


CommonSvc.chkLineInterZone = function (linePosLst, zone) {
    // int count = linePosLst.size();
    // if (count < 2){
    //     return false;
    // }

    // bool flag = false;
    // CustPoint *pPrePt = linePosLst.at(0);
    // CustPoint *pNextPt = null;
    // CLineSection lineSec;
    // for (int i = 1;i < count;i++){
    //     pNextPt = linePosLst.at(i);
    //     lineSec.stPoint = *pPrePt;
    //     lineSec.endPoint = *pNextPt;
    //     if (chkLineSecInterRect(lineSec,zone)){
    //         flag = true;
    //         break;
    //     }
    //     pPrePt = pNextPt;
    // }
    // return flag;
}

CommonSvc.chkLineSecInterRect = function (lineSec, rect) {
    //  if (rect.contains(lineSec.stPoint.x,lineSec.stPoint.y)
    //          || rect.contains(lineSec.endPoint.x,lineSec.endPoint.y)){
    //      return true;
    //  }
    //     CustPoint interPt;

    //      CLineSection zoneEadgeSec;

    //      //矩形上侧
    //      zoneEadgeSec.stPoint.x = rect.left;
    //      zoneEadgeSec.stPoint.y = rect.top;
    //      zoneEadgeSec.endPoint.x = rect.right;
    //      zoneEadgeSec.endPoint.y = rect.top;
    //      if (CYGUtilFuncs::getTwoLineSecInterPos(lineSec,zoneEadgeSec,interPt)){
    //          return true;
    //      }

    //      //矩形右侧
    //      zoneEadgeSec.stPoint.x = rect.right;
    //      zoneEadgeSec.stPoint.y = rect.top;
    //      zoneEadgeSec.endPoint.x = rect.right;
    //      zoneEadgeSec.endPoint.y = rect.bottom;
    //      if (CYGUtilFuncs::getTwoLineSecInterPos(lineSec,zoneEadgeSec,interPt)){
    //          return true;
    //      }

    //      //矩形下侧
    //      zoneEadgeSec.stPoint.x = rect.right;
    //      zoneEadgeSec.stPoint.y = rect.bottom;
    //      zoneEadgeSec.endPoint.x = rect.left;
    //      zoneEadgeSec.endPoint.y = rect.bottom;
    //      if (CYGUtilFuncs::getTwoLineSecInterPos(lineSec,zoneEadgeSec,interPt)){
    //          return true;
    //      }

    //      //矩形左侧
    //      zoneEadgeSec.stPoint.x = rect.left;
    //      zoneEadgeSec.stPoint.y = rect.bottom;
    //      zoneEadgeSec.endPoint.x = rect.left;
    //      zoneEadgeSec.endPoint.y = rect.top;
    //      if (CYGUtilFuncs::getTwoLineSecInterPos(lineSec,zoneEadgeSec,interPt)){
    //          return true;
    //      }
    //     return false;
}


export { CommonSvc }